/*
 * @(#) $CVSHeader:  $
 *
 * Copyright (C) 2010 by Netcetera AG.
 * All rights reserved.
 *
 * The copyright to the computer program(s) herein is the property of
 * Netcetera AG, Switzerland.  The program(s) may be used and/or copied
 * only with the written permission of Netcetera AG or in accordance
 * with the terms and conditions stipulated in the agreement/contract
 * under which the program(s) have been supplied.
 *
 * @(#) $Id: XmlPullFeedParser.java 80 2010-06-17 20:00:08Z lpancevski $
 */
package com.dreamcode.anfeedreader.utils;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;


public class XmlPullFeedParser extends BaseFeedParser {
  public XmlPullFeedParser(String feedUrl) {
      super(feedUrl);
  }
  
  public List<Message> parse() {
      List<Message> messages = null;
      XmlPullParser parser = Xml.newPullParser();
      try {
          // auto-detect the encoding from the stream
          parser.setInput(this.getInputStream(), null);
          int eventType = parser.getEventType();
          Message currentMessage = null;
          boolean done = false;
          while (eventType != XmlPullParser.END_DOCUMENT && !done){
              String name = null;
              switch (eventType){
                  case XmlPullParser.START_DOCUMENT:
                      messages = new ArrayList<Message>();
                      break;
                  case XmlPullParser.START_TAG:
                      name = parser.getName();
                      if (name.equalsIgnoreCase(ITEM)){
                          currentMessage = new Message();
                      } else if (currentMessage != null){
                          if (name.equalsIgnoreCase(LINK)){
                              currentMessage.setLink(parser.nextText());
                          } else if (name.equalsIgnoreCase(DESCRIPTION)){
                              currentMessage.setDescription(parser.nextText());
                          } else if (name.equalsIgnoreCase(PUB_DATE)){
                              currentMessage.setDate(parser.nextText());
                          } else if (name.equalsIgnoreCase(TITLE)){
                              currentMessage.setTitle(parser.nextText());
                          }
                      }
                      break;
                  case XmlPullParser.END_TAG:
                      name = parser.getName();
                      if (name.equalsIgnoreCase(ITEM) && currentMessage != null){
                          messages.add(currentMessage);
                      } else if (name.equalsIgnoreCase(CHANNEL)){
                          done = true;
                      }
                      break;
              }
              eventType = parser.next();
          }
      } catch (Exception e) {
          return null;
      }
      return messages;
  }
}

